// pages/my/order-detail/order-detail.js
const controllers = require('../../controllers/controllerOrder').controller
const app = getApp()

function InitTime(endtime) {
  var dd, hh, mm, ss = null;
  var time = (new Date(endtime.replace(/-/g,'/')).getTime() - new Date().getTime()) / 1000;
  if (time <= 0) {
    return '结束'
  } else {
    dd = Math.floor(time / 60 / 60 / 24);
    hh = Math.floor((time / 60 / 60) % 24);
    mm = Math.floor((time / 60) % 60);
    ss = Math.floor(time % 60);
    var str = dd + "天" + hh + ":" + mm + ":" + ss;
    return str;
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ordNo: 0,
    state: 0,
    goodsList: [],
    imgurl: '',
    account: {},
    shopAccount: {},
    createTime: '',
    payType: '',
    desc: '',
    money: '',
    orderComment: {},
    marketAccountList: [],
    sales: [],
    salesname: [],
    isshow: false,
    buttons: [{
      text: '取消'
    }, {
      text: '确定'
    }],
    singlesales: '',
    singleordno: '',
    singlesalesname: '',
    downtime: '',
    timer: ''
  },
    //预览图片
    topre(e){
      wx.previewImage({
        urls: [e.currentTarget.id],
      })
    },
  //按钮事件
  tapDialogButton(e) {
    console.log(e);
    if (e.detail.index == 0) {
      this.setData({
        isshow: false
      })
    } else {
        //提交
        controllers.serviceComplete({
          orderNo: this.data.ordNo,
          shopId: wx.getStorageSync('shopId'),
          shopAccountId: this.data.singlesales,
          type: 1
        }).then(res => {
          console.log(res);
          this.setData({
            isshow: false
          })
          //获取订单详情
          controllers.serviceorderDetail({
            shopId: wx.getStorageSync('shopId'),
            orderNo: this.data.ordNo,
            type: 1
          }).then(res => {
            console.log(res);
            this.setData({
              goodsList: res.data.goodsList,
              account: res.data.account,
              shopAccount: res.data.shopAccount,
              marketAccountList: res.data.marketAccountList,
              createTime: res.data.createTime,
              payType: res.data.payType,
              desc: res.data.desc,
              money: res.data.money,
              orderComment: res.data.orderComment,
              detail: res.data,
              state:res.data.state
            })

            if (res.data.payType == 0) {
              this.setData({
                payType: '支付宝'
              })
            } else if (res.data.payType == 1) {
              this.setData({
                payType: '微信'
              })
            } else if (res.data.payType == 2) {
              this.setData({
                payType: '余额'
              })
            } else if (res.data.payType == 3) {
              this.setData({
                payType: '积分'
              })
            } else {
              this.setData({
                payType: '现金'
              })
            }
          })
        })
    }
  },
  //选择销售
  // choice(e) {
  //   console.log(e);

  //   this.setData({
  //     singlesales: this.data.sales[e.detail.value].id,
  //     singlesalesname: this.data.sales[e.detail.value].nick
  //   })
  // },
  //打开选择销售弹框
  toChoiceSales() {
    this.setData({
      singleordno:this.data.ordNo
    })
      controllers.serviceComplete({
        orderNo: this.data.ordNo,
        shopId: wx.getStorageSync('shopId'),
        shopAccountId: this.data.staffId||"",
        type: 1
      }).then(res => {
        console.log(res);
        this.setData({
          isshow: false
        })
        //获取订单详情
        controllers.serviceorderDetail({
          shopId: wx.getStorageSync('shopId'),
          orderNo: this.data.ordNo,
          type: 1
        }).then(res => {
          console.log(res);
          this.setData({
            goodsList: res.data.goodsList,
            account: res.data.account,
            shopAccount: res.data.shopAccount,
            marketAccountList: res.data.marketAccountList,
            createTime: res.data.createTime,
            payType: res.data.payType,
            desc: res.data.desc,
            money: res.data.money,
            orderComment: res.data.orderComment,
            detail: res.data,
            state:res.data.state
          })

          if (res.data.payType == 0) {
            this.setData({
              payType: '支付宝'
            })
          } else if (res.data.payType == 1) {
            this.setData({
              payType: '微信'
            })
          } else if (res.data.payType == 2) {
            this.setData({
              payType: '余额'
            })
          } else if (res.data.payType == 3) {
            this.setData({
              payType: '积分'
            })
          } else {
            this.setData({
              payType: '现金'
            })
          }
        })
      })
  },

   // 复制订单
   copyOrder() {
    const detail = this.data.detail
    const orderNo = this.data.ordNo
    const money = detail.type == 4 ? (detail.integral + '积分') : (detail.money + '元')

    const data = `下单用户:${detail.account.nick},联系方式:${detail.account.mobile},收货地址:${detail.address ?? '到店自取'},订单号:${orderNo},跟单员工:${detail.shopAccountNick},实付款:${money}`

    wx.setClipboardData({
      data
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      imgurl: app.globalData.IMG_URL,
      sales: wx.getStorageSync('sales')
    })
    // let salesname = []
    // let sales = wx.getStorageSync('sales')
    // if(sales){
    //   sales.forEach(function (item, index) {
    //     salesname.push(item.nick)
    //   })
    // }
    if(!wx.getStorageSync('shopId')||!wx.getStorageSync('token')){
      wx.redirectTo({
        url: '/pages/login/index',
      })
    }else{
      this.setData({
        ordNo: options.id,
        state: "",
        // salesname: salesname
      })
    }
    //获取订单详情
    controllers.serviceorderDetail({
      shopId: wx.getStorageSync('shopId'),
      orderNo: this.data.ordNo,
      type: 1
    }).then(res => {
      console.log(res);
      this.setData({
        goodsList: res.data.goodsList,
        account: res.data.account,
        shopAccount: res.data.shopAccount,
        marketAccountList: res.data.marketAccountList,
        createTime: res.data.createTime,
        payType: res.data.payType,
        desc: res.data.desc,
        money: res.data.money,
        orderComment: res.data.orderComment,
        detail: res.data,
        state:res.data.state
      })
      // setInterval(() => {

      // }, 1000);
      if (this.data.detail.marketAccountList && this.data.detail.marketAccountList[0].endTime) {
        this.setData({
          timer: setInterval(() => {
            this.data.downtime = InitTime(this.data.detail.marketAccountList[0].endTime)
            this.setData({
              downtime: this.data.downtime
            })
          }, 1000)
        })
      }
      if (res.data.payType == 0) {
        this.setData({
          payType: '支付宝'
        })
      } else if (res.data.payType == 1) {
        this.setData({
          payType: '微信'
        })
      } else if (res.data.payType == 2) {
        this.setData({
          payType: '余额'
        })
      } else if (res.data.payType == 3) {
        this.setData({
          payType: '积分'
        })
      } else {
        this.setData({
          payType: '现金'
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getStaffList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.data.timer)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // }
  //获取员工列表
  getStaffList(){
    let shopId=wx.getStorageSync('shopId')
    controllers.getStaffList({shopId:shopId}).then(res=>{
      this.setData({
        list:res.data
      })
    })
  },


  //关闭员工选框
  closeOpen(){
    this.setData({
      staffBox:false
    })
  },

  //显示弹框
  showModal: function() {
    this.setData({
      showModalStatus: true
    })
  },
  // 隐藏弹框
  hideModal: function() {
      this.setData({
        showModalStatus: false
      })
  },

  //选择员工
  selectStaff(e){
    let index=e.currentTarget.dataset.index
    console.log(e)
    if(index===this.data.selectIndex){
      this.setData({
        selectIndex:"",
        staffId:"",
        staffName:""
      })
    }else{
      this.setData({
        selectIndex:index
      })
    }
  },

  //确定员工
  defineStaff(){
    if(this.data.selectIndex||this.data.selectIndex===0){
      this.setData({
        staffId:this.data.list[this.data.selectIndex].id,
        staffName:this.data.list[this.data.selectIndex].nick
      })
    }
    this.toChoiceSales()
    this.hideModal()
  },

  //跳转员工详情
  selectStaffDetail(e){
    let id=e.currentTarget.dataset.id
    let index=e.currentTarget.dataset.index
    let value=this.data.sales[index]
    wx.navigateTo({
      url: '/pages/selecStaffDetail/selecStaffDetail?staffDetail='+encodeURIComponent(JSON.stringify(value)),
    })
  }
})